package vn.appsmobi.loader;

import java.util.ArrayList;

import org.json.JSONObject;

import vn.appsmobi.model.AppCommentInfo;
import vn.appsmobi.ulti.Constant;
import vn.appsmobi.ulti.Log;
import vn.appsmobi.ulti.Request;
import android.content.Context;
import android.text.TextUtils;

public class AppCommentsLoader extends BaseLoader
{

  private String mAppId;
  public boolean mNeedNextPage;
  public int mPage = 1;

    public AppCommentsLoader(Context context) {
		super(context);	
		this.mNeedNextPage = false;
		Log.LOGTAG = "AppCommentsLoader";
	}

	@Override
	protected BaseResult getResultInstance() {
		 return new Result();
	}
	
	@Override
	protected BaseResult parseResult(JSONObject json, BaseResult result)
			throws Exception {
		AppCommentsLoader.Result localResult = null;
		if(json != null)
		{
		  localResult = new AppCommentsLoader.Result();
		  localResult.mCommentTotalCount = json.optInt("commentCount");
          localResult.mScoreTotalCount = json.optInt("pointCount");
          localResult.mScore = json.optDouble("score");
          localResult.mCountOfPoints = new int[5];
          localResult.mCountOfPoints[0] = json.optInt("point1Count");
          localResult.mCountOfPoints[1] = json.optInt("point2Count");
          localResult.mCountOfPoints[2] = json.optInt("point3Count");
          localResult.mCountOfPoints[3] = json.optInt("point4Count");
          localResult.mCountOfPoints[4] = json.optInt("point5Count");
          //localResult.mHasBought = json.optBoolean("hasBoughtApp", false);
          ArrayList<AppCommentInfo> commentList = AppCommentInfo.valueOf(json);
          if (commentList != null)
          {
            localResult.mAppCommentsList = commentList;
          }
		}

		return localResult;
			
	}
	
	
	protected boolean dataExists()
	  {
		Log.v("dataExists");
	    if (super.dataExists())
	      if (mPage == 1 ||((((Result)this.mResult).mAppCommentsList != null) && ((ArrayList<AppCommentInfo>)((AppCommentsLoader.Result)this.mResult).mAppCommentsList).size() > 0) )
	      {
	    		Log.v("dataExists:"  + ((ArrayList<AppCommentInfo>)((AppCommentsLoader.Result)mResult).mAppCommentsList).size());		    	
	    	  return true;
	      }
	      
		return false;
	       
	  }
	
	
	@Override
	protected BaseLoader.UpdateTask getUpdateTask() {
		// TODO Auto-generated method stub
		return new AppCommentUpdateTask();
	}
	
	
	private class AppCommentUpdateTask extends BaseLoader.UpdateTask
    {
		protected boolean mIsAppend;
        private AppCommentUpdateTask()
        {
            super();
            if (AppCommentsLoader.this.mPage == 1)
            	this.mIsAppend =  false;
            else
            	this.mIsAppend =  true;
        }

		@Override
		protected Request getRequest() {
			Log.v(String.format(Constant.GET_LISCOMMENT_URL,AppCommentsLoader.this.mAppId,mPage));
			return new Request(String.format(Constant.GET_LISCOMMENT_URL,AppCommentsLoader.this.mAppId,mPage));
		}
		
		
		private AppCommentsLoader.Result merge(AppCommentsLoader.Result oldresult, AppCommentsLoader.Result newresult)
	    {
	      AppCommentsLoader.Result localResult = new AppCommentsLoader.Result();
	      localResult.mCommentTotalCount = newresult.mCommentTotalCount;
	      localResult.mScoreTotalCount = newresult.mScoreTotalCount;
	      localResult.mScore = newresult.mScore;
	      localResult.mAppCommentsList = new ArrayList<AppCommentInfo>();
	      Log.v("merge 1:"  + ((ArrayList<AppCommentInfo>)((AppCommentsLoader.Result)mResult).mAppCommentsList).size());
	        if (oldresult != null)
	          localResult.mAppCommentsList.addAll(oldresult.mAppCommentsList);
	        if (newresult != null)
	           localResult.mAppCommentsList.addAll(newresult.mAppCommentsList);
	        
	        Log.v("merge 2:"  + ((ArrayList<AppCommentInfo>)((AppCommentsLoader.Result)mResult).mAppCommentsList).size());
	        
		  return localResult;
	        
	    }

	    /*protected boolean compareResult(AppCommentsLoader.Result oldresult, AppCommentsLoader.Result newresult)
	    {
	      
	    	if ((oldresult == null) && (newresult == null))
	    	  return false;	 
	        else if (oldresult.mAppCommentsList.size() != newresult.mAppCommentsList.size())
	         return false;
	        else if (oldresult.mScore != newresult.mScore)
	        	return false;
	        else if (oldresult.mCommentTotalCount != newresult.mCommentTotalCount)
	        	return false;
	        else if (oldresult.mScoreTotalCount != newresult.mScoreTotalCount)
	        	return false;
	        else
	          for (int i = 0; i < oldresult.mAppCommentsList.size() ; i++)
	          {	          
	            AppCommentInfo commentInfo1 = (AppCommentInfo)oldresult.mAppCommentsList.get(i);
	            AppCommentInfo commentInfo2 = (AppCommentInfo)newresult.mAppCommentsList.get(i);
	            if ((!TextUtils.equals(commentInfo1.mId, commentInfo2.mId)) || (commentInfo1.mTime != commentInfo2.mTime))
	            {
		            return false;		           
	            }
	          }
			return true;	      
	    }
	    */
	    protected BaseResult onDataLoaded(BaseResult oldresult, BaseResult newresult)
	    {
	    	BaseResult localResult = null;
	    	
	    	if(newresult != null && (((Result)newresult).mAppCommentsList != null) && (!((Result)newresult).mAppCommentsList.isEmpty()))
	    	{
	    		AppCommentsLoader.this.mNeedNextPage = true;
	    		localResult = newresult;
	    		
	    		mResult = newresult.shallowClone();
	    		Log.v("this.mIsAppend:" + this.mIsAppend);
	    		if(this.mIsAppend )
		    	{
		    		localResult = merge((AppCommentsLoader.Result)oldresult, (AppCommentsLoader.Result)newresult);
		    	}
	
	    		return localResult;
	    	}
	    	
	    	
	    	return newresult;

	    }
           
    }
	
	public static final class Result extends BaseResult
    {
        public ArrayList<AppCommentInfo> mAppCommentsList;
        public int mCommentTotalCount;
        public int[] mCountOfPoints;
        public boolean mHasBought = false;
        public double mScore;
        public int mScoreTotalCount;
        
        protected int getCount()
        {
        	if(this.mAppCommentsList != null)
        		return 1;
        	return 0;
        }
        
        public BaseResult shallowClone()
        {
            Result result = new Result();
            result.mAppCommentsList = this.mAppCommentsList;
            result.mCommentTotalCount = this.mCommentTotalCount;
            result.mScoreTotalCount = this.mScoreTotalCount;
            result.mScore = this.mScore;
            result.mCountOfPoints = this.mCountOfPoints;
            result.mHasBought = this.mHasBought;
            return result;
        }
    }
	
	public void reload()
	  {
	    if (!isLoading())
	    {
	      this.mPage = 0;
	      this.mNeedNextPage = false;
	      super.reload();
	    }
	  }
	
	public void nextPage() {
		if (this.mNeedNextPage)
		      this.mPage = (1 + this.mPage);
		
	}

	public void setAppId(String mAppId2) {
		mAppId = mAppId2;
		
	}

	
}

